
// 是否测试接口 true 测试
export const testData = true;
// export const interface_url = testData ? "/apis" : "https://www.baidu.com";
// export const interface_url = testData ? " http://122.51.200.161:2021/TCLCar" : "https://www.baidu.com";
export const interface_url = " http://122.51.200.161:2021/TCLCar" ;

import {Toast} from 'mint-ui'

// toast 写成 单例  第二个弹出来 上一个消失
var instanceToast;
export const toast = function(title) {
    if (instanceToast){
        instanceToast.close()
    }
    instanceToast = Toast({
        message: title,
        duration: 800,
    })
}

export const date_format = (micro_second)=>{
    // 秒数
    var second = Math.floor(micro_second / 1000);
    //天位
    var dy = Math.floor(second / (60 * 60 * 24));
    // 小时位
    var hr = formatNumbers(Math.floor(second / (60 * 60)) - (dy * 24));
    // 分钟位
    var min = formatNumbers(Math.floor(second / 60) - (dy * 24 * 60) - (hr * 60));
    // 秒位
    var sec = formatNumbers(Math.floor(second) - (dy * 24 * 60 * 60) - (hr * 60 * 60) - (min * 60));// equal to => var sec = second % 60;
    // 毫秒位，保留2位
    var micro_sec = formatNumbers(Math.floor((micro_second % 1000) / 10));
    var all = {
        "dy": dy,
        "hr": hr,
        "min": min,
        "sec": sec,
        "msec": micro_sec
    };
    return all;
};
export const formatNumbers = (n) =>{
    n = n.toString();
    return n[1] ? n : '0' + n
};
// 时间戳转化为时分秒
export const conversion_hh_ss = (dates) => {
    let newDate = new Date(dates);
    let date;
    let t;
    let fen;
    let miao;
    let Y;
    let M;
    let D;
    let h;
    let s;
    let m;
    Y = newDate.getFullYear()+'-';
    M = (newDate.getMonth()+1 < 10 ? '0'+(newDate.getMonth()+1) :newDate.getMonth()+1) + '-';
    D = newDate.getDate();
    if(D<10){
        date = '0'+D+' '
    }else{
        date = D+ ' '
    }
    h = newDate.getHours()
    if(h< 10){
        t = '0' + h + ':';
    }else{
        t=h+':'
    }
    m = newDate.getMinutes()
    if(m < 10){
        fen= '0'+m+':';
    }else{
        fen= m+':';
    }
    s = newDate.getSeconds();
    if(s<10){
        miao= '0'+s
    }else{
        miao= s
    }
    return Y+M+date+t+fen+miao;

};


// 判断输入是否为合法的手机号码
export const isphoneNumber = (inputString) => {
    var partten = /^1[3-9]\d{9}$/
    if (partten.test(inputString)) {
        return true
    } else {
        return false
    }
};

// 手机号码中间四位为*
export const phoneReplace = (phone) => {
    var tel = "" + phone;
    var reg=/(\d{3})\d{4}(\d{4})/;
    var phoneNum = tel.replace(reg, "$1****$2")
    return phoneNum
};
//银行卡号中间*好展示
export const formatBankNumber = (bankNumber) => {
  return bankNumber.substr(0,6)+" "+"*** ***"+bankNumber.substr(-4);

};

// 判断是否是两位小数
export const checktwoPoint = (inputNumber) => {
    var partten = /^\d+(\.\d{1,2})?$/
    if (partten.test(inputNumber)) {
        return true
    } else {
        return false
    }
};
//制保留2位小数，如：2，会在2后面补上00.即2.00
export const toDecimal2 = (x) => {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
};
// 元转化为分param
export const fen = (yuan) => {
    return parseFloat(yuan) * 1000 * 100 / 1000;
};
// 分转化元展示
export const yuan = (fen) => {
    return toDecimal2(parseFloat(fen) * 1000 / 100 / 1000);
};

// 将对象转换为数组
export const  transform = (obj) =>{
    var arr = [];
    for(let item in obj){
        let goodAttr = {
            key:item,
            value:obj[item]
        };
        arr.push(goodAttr);
    }
    return arr;
};
// 判空
export const isNull = (args) => {
    if (args === null || args === 'null' || args === undefined || args === 'undefined' || args === '' || args.length === 0) {
        return true
    } else {
        return false
    }
};
// 银行卡校验
export const isBankCardNo = (bankNo) => {
    var num = /^\d*$/
    if (bankNo.length < 16 || bankNo.length > 19 || !num.test(bankNo)) {
        return false
    }
    return true
};
// 身份证验证
export const ispCardNo = (cardNo) => {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (reg.test(cardNo) === false) {
        return false
    }
    return true
};
// 检查图片是否为全路径
export const changeImgUrl = (newUrl) => {
    let isChange = newUrl.indexOf("http");
    let urlPrefix = "http://yuanmaxi.com/";
    if (isChange > -1) {
        return newUrl;
    } else if (isChange < 0) {
        return urlPrefix + newUrl;
    }
};

//时间戳转化为时间
export const formatTime = (date) =>{
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
};
export const formatNumber = (n) => {
    n = n.toString()
    return n[1] ? n : '0' + n
};


//微信授权获取头像和昵称
// export const setAuthorize = () =>{
//     let token = window.localStorage.getItem("token")
//     window.location.href = interface_url + '/user/auth'
// }

// 返回字段json
export const JsonParse = (data) => {
    try {
        return JSON.parse(data)
    } catch (e) {
        return null
    }
};
// 从数组中删除指定的元素
export const removeByValue = (arr, val) =>{
    for(var i=0; i<arr.length; i++) {
        if(arr[i] === val) {
            arr.splice(i, 1);
            break;
        }
    }
    return arr
};

// 从cookie中获取值
export const getCookie = (sName) => {
    var aCookie = document.cookie.split("; ");
    for (var i=0; i < aCookie.length; i++)
    {
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return aCrumb[1];
    }
    return null;
};
// 删除指定cookie
export function delCookie (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}
// 删除所有cookie
export const delAllCookie = () =>{
    var myDate=new Date();
    myDate.setTime(-1000);//设置时间
    var data=document.cookie;
    var dataArray=data.split("; ");
    for(var i=0;i<dataArray.length;i++){
        var varName=dataArray[i].split("=");
        document.cookie=varName[0]+"=''; expires="+myDate.toGMTString();
    }
};
// 去掉空格
export const trim =(str) => {
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g,"");
    result = result.replace(/\s/g,"");
    return result;
}

//手机号格式
export const konggeString =(data) => {
    var kongge_phone = data.replace(/\s*/g, "");
    var result = [];
    for (var i = 0; i < kongge_phone.length; i++) {

        if (i == 3 || i == 7) {
            result.push(" " + kongge_phone.charAt(i));
        }
        else {
            result.push(kongge_phone.charAt(i));
        }
    }
    kongge_phone = result.join("");
    return kongge_phone
};

//校验qq号
export const isQQ= (qq) => {
    qq = qq + "";
    var reg = /^[1-9]\d{4,9}$/;
    if (reg.test(qq)) {
        if (qq <= 3750000000 && qq >= 10000) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
};
/// 深copy
export const mutableCopy = function(source) {
    var result={};
    for (var key in source) {
        result[key] = typeof source[key]==='object'? mutableCopy(source[key]): source[key];
    }
    return result;
};
function pZe(s){
  return s < 10 ? '0' + s : s
}

/// js格式化 Thu Oct 31 2019 15:49:15 GMT+0800 (CST)
export const geShiHua = function(time){
  var d = new Date(time);
  var resDate = d.getFullYear() + '-' + pZe((d.getMonth() + 1)) + '-' + pZe(d.getDate());
  var resTime = pZe(d.getHours()) + ':' + pZe(d.getMinutes()) + ':' + pZe(d.getSeconds());
  return resDate + " " +resTime;
};
// 校验是不是数字的
export const checknum = function(num){
  if(isNaN(num)){
    Toast("请输入数字");
    return false;
  }
};
// 数组去掉数组里面的undefind
export const isArrUndefind = function(param){
  for(var i=0;i<param.length;i++){
    if(param[i] === undefined ){
      param.splice(i,1);
      i=i-1
    }
  }
  return param;
};

export const checkPassword = function(password){
  return /^\d{6}$/.test(password)
}

export const checkCarNum = function (str) {
  return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(str);
}

export const isEmpty = function(str) {
  if (str === undefined || str.trim() === '') {
    return true;
  } else {
    return false;
  }
}
